package com.hbnu.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbnu.system.config.BaseException;
import com.hbnu.system.mapper.ZCfundsMapper;
import com.hbnu.system.model.entity.Tranproject_registration;
import com.hbnu.system.model.entity.ZCfunds;
import com.hbnu.system.service.ITranproject_registrationService;
import com.hbnu.system.service.IUserService;
import com.hbnu.system.service.ZCfundsService;
import com.hbnu.system.utils.CheckRoleUtil;
import com.hbnu.system.utils.RegexPatterns;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class ZCfundsServiceImpl extends ServiceImpl<ZCfundsMapper, ZCfunds> implements ZCfundsService {
    @Autowired
    private ZCfundsService zcfundsService;
    @Autowired
    private ITranproject_registrationService iTranproject_registrationService;
    @Autowired
    private IUserService iUserService;
    @Autowired
    private CheckRoleUtil checkRoleUtil;

    /**
     * 添加支出经费
     * @param zcfunds 支出经费
     * @return boolean
     */
    @Override
    public boolean addZCfundsProject(ZCfunds zcfunds) {
        zcfunds.setZcfundsID(null);
        // 检查 zcfunds 是否为空
        if (zcfunds == null) {
            throw new BaseException("支出经费对象不能为空！");
        }
        // 查询横向项目
        String tregistrationID = zcfunds.getTregistrationID();
        if (StrUtil.isEmpty(tregistrationID) || StrUtil.isBlank(tregistrationID)) {
            throw new BaseException("请输入横向项目id!");
        }
        Tranproject_registration tranProjectRegistration = iTranproject_registrationService.getById(tregistrationID);
        if (tranProjectRegistration == null) {
            throw new BaseException("未找到对应的横向项目！");
        }
        checkRoleUtil.checkPermissionProject(tranProjectRegistration.getProjectState());
        if (StrUtil.isEmpty(zcfunds.getZcfundsNum()) || StrUtil.isBlank(zcfunds.getZcfundsNum())) {
            throw new BaseException("请输入支出编号!");
        }
        // 校验经费是否输入的是数字
        String zcfundsAmount = zcfunds.getZcfundsAmount();
        if (StrUtil.isEmpty(zcfundsAmount) || StrUtil.isBlank(zcfundsAmount)) {
            throw new BaseException("请输入支出金额!");
        }
        if (!zcfundsAmount.matches(RegexPatterns.NUMBER_REGEX)) {
            throw new BaseException("经费请输入数字");
        }
        ZCfunds zcfunds1 = zcfundsService.getOne(Wrappers.lambdaQuery(ZCfunds.class)
                .eq(ZCfunds::getZcfundsNum, zcfunds.getZcfundsNum()));
        if (!Objects.isNull(zcfunds1)) {
            throw new BaseException("支出编号已被使用");
        }
        if (StrUtil.isEmpty(zcfunds.getZcfundsType()) || StrUtil.isBlank(zcfunds.getZcfundsType())) {
            throw new BaseException("请输入支出类别!");
        }
        if (Objects.isNull(zcfunds.getZcfundsDate())) {
            throw new BaseException("请输入到账时间!");
        }
        zcfunds.setUploadTime(LocalDateTime.now());
        return baseMapper.insert(zcfunds) == 1;
    }

    /**
     * 修改支出经费
     * @param zcfunds
     * @return
     */
    @Override
    public boolean updateZCfundsProject(ZCfunds zcfunds) {
        //查询横向项目
        Tranproject_registration tranProjectRegistration = iTranproject_registrationService.getById(zcfunds.getTregistrationID());
        checkRoleUtil.checkPermissionProject(tranProjectRegistration.getProjectState());
        if(StrUtil.isEmpty(zcfunds.getTregistrationID()) || StrUtil.isBlank(zcfunds.getTregistrationID())){
            throw new BaseException("请输入横向项目id!");
        }
        if(StrUtil.isEmpty(zcfunds.getZcfundsNum()) || StrUtil.isBlank(zcfunds.getZcfundsNum())){
            throw new BaseException("请输入支出编号!");
        }
        //校验经费是否输入的是数字
        if (!zcfunds.getZcfundsAmount().matches(RegexPatterns.NUMBER_REGEX)) {
            throw new BaseException("经费请输入数字");
        }
        ZCfunds zcfunds1 = zcfundsService.getOne(Wrappers.lambdaQuery(ZCfunds.class)
                .eq(ZCfunds::getZcfundsNum,zcfunds.getZcfundsNum())
                .ne(ZCfunds::getZcfundsID,zcfunds.getZcfundsID()));
        if(!Objects.isNull(zcfunds1)){
            throw new BaseException("支出编号已被使用");
        }
        if(StrUtil.isEmpty(zcfunds.getZcfundsType()) || StrUtil.isBlank(zcfunds.getZcfundsType())){
            throw new BaseException("请输入支出类别!");
        }
        if(StrUtil.isEmpty(zcfunds.getZcfundsAmount()) || StrUtil.isBlank(zcfunds.getZcfundsAmount())){
            throw new BaseException("请输入支出金额!");
        }
        if(Objects.isNull(zcfunds.getZcfundsDate())){
            throw new BaseException("请输入到账时间!");
        }
        zcfunds.setUploadTime(LocalDateTime.now());
        return baseMapper.updateById(zcfunds)==1;
    }

    /**
     * 删除支出经费
     * @param zcfundsID
     * @return
     */
    @Override
    public boolean removeZCfundsProject(String zcfundsID) {
        ZCfunds zcfunds = super.getOne(Wrappers.lambdaQuery(ZCfunds.class)
                .eq(ZCfunds::getZcfundsID,zcfundsID));
        if(Objects.isNull(zcfunds)){
            throw new BaseException("不存在该支出经费");
        }
        Tranproject_registration tranProjectRegistration = iTranproject_registrationService.getById(zcfunds.getTregistrationID());
        checkRoleUtil.checkPermissionProject(tranProjectRegistration.getProjectState());
        return baseMapper.deleteById(zcfunds) == 1;


    }

    @Override
    public List<ZCfunds> getZCfundsByTranprojectID(String tranprojectID) {
        Tranproject_registration tranproject_registration = iTranproject_registrationService.getById(tranprojectID);
        if(Objects.isNull(tranproject_registration)){
            throw new BaseException("输入的横向项目编号有误");
        }
        List<ZCfunds> zcfunds = baseMapper.selectList(Wrappers.lambdaQuery(ZCfunds.class)
                .eq(ZCfunds::getTregistrationID,tranprojectID));
        return zcfunds;
    }


}
